package org.example.designPattern.structural.composite;

import net.sf.cglib.core.CollectionUtils;

import java.util.List;

/*
* 组合模式：对单一对象、组合对象统一处理
* */
public class CompositeTest {
    public static void main(String[] args) {
        // 创建公司所有职员
        Leader a = new Leader("A", "董事长", 100000);
        Leader b = new Leader("B", "技术总监", 70000);
        Leader c = new Leader("C", "人事总监", 60000);
        Leader d = new Leader("D", "财务总监", 50000);
        GrassrootsEmployees b1 = new GrassrootsEmployees("B1", "程序员", 30000);
        GrassrootsEmployees b2 = new GrassrootsEmployees("B2", "程序员", 20000);
        GrassrootsEmployees c1 = new GrassrootsEmployees("C1", "HR", 15000);
        GrassrootsEmployees d1 = new GrassrootsEmployees("D1", "会计", 13000);

        // a下面的员工
        a.setGrassrootsEmployees(b, c, d);
        // b、c、d下面的员工
        b.setGrassrootsEmployees(b1, b2);
        c.setGrassrootsEmployees(c1);
        d.setGrassrootsEmployees(d1);

        // 获得下属信息
        System.out.println("---------董事长的下属----------");
        getGrassrootsEmployees(a);
        System.out.println("---------技术总监的下属----------");
        getGrassrootsEmployees(b);
    }

    private static void getGrassrootsEmployees(Leader leader) {
        if(leader!=null){
            List<Employees> grassrootsEmployeesList = leader.getGrassrootsEmployeesList();
            if(grassrootsEmployeesList!=null && !grassrootsEmployeesList.isEmpty()){
                for (Employees employees : grassrootsEmployeesList) {
                    System.out.println(employees);
                    if(employees instanceof Leader){
                        Leader l2 = (Leader) employees;
                        getGrassrootsEmployees(l2);
                    }
                }
            }
        }

    }
}
